package com.portal.back.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpSession;

import com.portal.back.service.ISysAdminService;
import com.portal.back.service.impl.SysAdminService;
import com.portal.base.web.BaseAction;
import com.portal.domain.SysAdminUser;
import com.portal.util.common.CharacterUtil;
import com.portal.util.common.DateTimeUtil;

/**
 * @Title:管理员Action
 * @Copyright: Copyright (c) 0.1
 * @author:鲁根成
 * @version: 1.0
 * @time: 2011-3-25
 * 
 */
@SuppressWarnings("serial")
public class SysAdminAction extends BaseAction {

	private ISysAdminService service;
	private String name;
	private String password;
	private String level;

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public SysAdminAction() {
		service = new SysAdminService();
	}

	/**
	 * 
	 * 管理员login
	 */
	public String adminlogin() {
		response.setContentType("text/html;charset=gbk");
		PrintWriter out = null;
		String a = request.getParameter("username");
		String userName = "";
		try {
			userName = URLDecoder.decode(a, "utf-8");
		} catch (UnsupportedEncodingException e1) {

			e1.printStackTrace();
		}
		String ip = request.getRemoteAddr();
		String dateString = DateTimeUtil.getCurrentDate2();
		SysAdminUser user = service.getLoginSuccess(userName, password, ip,
				dateString);
		if (user == null) {
			message = "0";

		} else {
			message = "1";
			HttpSession session = request.getSession();
			session.setAttribute("adminUser", user);

		}
		StringBuffer sb = new StringBuffer("[");
		sb.append("{\"message\":\"" + message + "\"" + "}");
		sb.append("]");

		try {
			out = response.getWriter();
			out.println(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 管理员退出
	 */
	public String goOut() {
		HttpSession session = request.getSession();
		session.removeAttribute("adminUser");
		return "success";
	}

	/**
	 * 修改管理员密码
	 */
	public String updatePass() {
		response.setContentType("text/html;charset=gbk");
		PrintWriter out = null;
		String newPassword = request.getParameter("newPassword");
		HttpSession session = request.getSession();
		SysAdminUser user = (SysAdminUser) session.getAttribute("adminUser");
		user.setPassword(newPassword);
		boolean isflag = service.updatePass(user);
		if (isflag)
			message = "1";
		else
			message = "0";
		StringBuffer sb = new StringBuffer("[");
		sb.append("{\"message\":\"" + message + "\"" + "}");
		sb.append("]");

		try {
			out = response.getWriter();
			out.println(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获得用户的管理级别
	 */
	public String getLevle() {
		response.setContentType("text/html;charset=gbk");
		PrintWriter out = null;
		String id = request.getParameter("id");
		String level = service.getLeveleById(id);
		if (level != null)
			message = level;
		StringBuffer sb = new StringBuffer("[");
		sb.append("{\"message\":\"" + message + "\"" + "}");
		sb.append("]");

		try {
			out = response.getWriter();
			out.println(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 新增后台管理员用户
	 */
	public String addUser() {
		response.setContentType("text/html;charset=gbk");
		PrintWriter out = null;
		String old = request.getParameter("username");
		String username = CharacterUtil.encode(old);
		int count = service.count(username);
		if (count != 0) {
			message = "用户名已经存在";
		} else {
			SysAdminUser user = new SysAdminUser();
			user.setAdminName(username);
			user.setPassword(password);
			user.setLevel(level);
			boolean isFlag = service.save(user);
			if (isFlag)
				message = "新增成功";
			else
				message = "新增失败";
		}
		StringBuffer sb = new StringBuffer("[");
		sb.append("{\"message\":\"" + message + "\"" + "}");
		sb.append("]");

		try {
			out = response.getWriter();
			out.println(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
